package com.security.lasys.agent.plugin;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Vector;

/**
 * plugin的配置信息
 */
public class PluginConf {
    Logger logger = LoggerFactory.getLogger(this.getClass());

    private ConfigPraser configPraser;

    private HashMap<String,String> sectionMap;
    public boolean exits;
    public HashMap<String,HashMap<String,String>> configMap;

    //读取配置文件
    private boolean readFile(String filename){
        if (new File(filename).exists()){
            configPraser = new ConfigPraser(filename);
            return true;
        }else
            return false;
    }

    private String getFormatFile(String section,String option){
        String value = "";
        value = configPraser.read(section,option);
        value = stripValue(value);
        return value;
    }

    public HashMap<String,HashMap<String,String>> hitems(){
        configMap = new HashMap<>();

        for (int i = 0 ; i < configPraser.showTitles().size() ; i++){
            sectionMap = new HashMap<>();
            for (int k = 0 ; k < configPraser.showNames(
                    configPraser.showTitles().elementAt(i)).size() ; k++){
                sectionMap.put(
                        configPraser.showNames(configPraser.showTitles().elementAt(i)).elementAt(k),
                        getFormatFile(
                                configPraser.showTitles().elementAt(i),
                                configPraser.showNames(
                                        configPraser.showTitles().elementAt(i)).elementAt(k)));
            }
            configMap.put(configPraser.showTitles().elementAt(i),sectionMap);
        }
        return configMap;
    }

    public Boolean set(String section,String option,String value){
        configMap.get(section).put(option,value);
        return configPraser.set(section,option,value);
    }

    @SuppressWarnings("rawtypes")
    public Vector<String> getSections(){
        Vector<String> sections = new Vector<String>();
        Iterator i = configMap.keySet().iterator();
        while(i.hasNext()){
            sections.add((String) i.next());
        }
        return sections;
    }

    @SuppressWarnings("rawtypes")
    public Vector<String> getOptions(String section){
        Vector<String> options = new Vector<>();
        Iterator i = configMap.get(section).keySet().iterator();
        while (i.hasNext()){
            options.add((String) i.next());
        }
        return options;
    }

    public String get(String section,String option){
        if (this.hasSection(section) &&this.hasOption(section,option)){
            return configMap.get(section).get(option);
        }
        return null;
    }

    public boolean hasOption(String section,String option){
        if (!configMap.containsKey(section)){
            return false;
        } else if (!configMap.get(section).containsKey(option)) {
            return false;
        }
        return true;
    }

    public boolean hasSection(String section){
        if (!configMap.containsKey(section)){
            return false;
        } else
            return true;
    }

    public void add(String section, String Option, String value)
    {
        if (hasOption(section, Option)) {
            set(section, Option, value);
        } else {
            configPraser.add(section, Option, value);
            configMap.get(section).put(Option, value);
        }
    }

    public boolean remove(String section)
    {
        if (hasSection(section)) {
            configPraser.removeSection(section);
            configMap.get(section).clear();
            return true;
        } else
            return false;
    }

    public String stripValue(String value){
        if (value == "")
            return value;
        if (value.charAt(0) == '\"' || value.charAt(0) == '\''
                || value.charAt(value.length() - 1) == '\"'
                || value.charAt(value.length() - 1) == '\''){
            if (value.charAt(0) == '\"'){
                value = value.substring(1);
            }
            if (value.charAt(value.length() - 1) == '\"'){
                value = value.substring(0,value.length() - 1);
            }
            if (value.charAt(0) == '\''){
                value = value.substring(1);
            }
            if (value.charAt(value.length() - 1) == '\''){
                value = value.substring(0,value.length() - 1);
            }
        }
        return value;
    }

    public PluginConf(String configfile)
    {

        if (readFile(configfile)) {
            hitems();
            exits = true;
        } else {
            // System.out.println("None!");
            exits = false;
        }
    }

    public static void main(String[] args) {
//        System.out.println("asufbasubf");
    }
}
